'use strict';

App.provider('router',['$stateProvider', function ($stateProvider) {

    this.$get = function ($state) {
        return {
            setUpRoutes: function (menus) {
                var router = this;
                for (var i = 0; i < menus.length; i++) {
                    router.setUpRoute(menus[i]);
                }
            },
            setUpRoute: function(menu) {
                var state = 'auth.' + menu.menuUrl;
                if (menu.menuUrl != undefined && menu.menuUrl.length > 0) {
                    if (!$state.get(state)) {
                        $stateProvider.state(state, {
                            url: "/" + menu.menuUrl,
                            templateUrl: 'views/' + menu.templateUrl,
                            data: {pageTitle: menu.menuName, menuId: menu.menuId},
                            controller: menu.ctrlName,
                            resolve: menu.ctrlPath && menu.ctrlPath.length > 0 ? {
                                deps: ['$ocLazyLoad', function ($ocLazyLoad) {
                                    return $ocLazyLoad.load({
                                        name: 'App',
                                        insertBefore: '#ng_load_plugins_before', // load the above css files before '#ng_load_plugins_before'
                                        files: [
                                            'js/controllers/' + menu.ctrlPath
                                        ]
                                    });
                                }]
                            } : {}
                        });
                    }
                }
                if (menu.children) {
                    for (var i = 0; i < menu.children.length; i++) {
                        this.setUpRoute(menu.children[i]);
                    }
                }
            }
        }
    };
}]);

/* Setup Rounting For All Pages */
App.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    // Redirect any unmatched url
    $urlRouterProvider.otherwise(function($injector, $location) {
       		$location.path('/index.html');
    });

    $stateProvider
    	.state('login', {
            url: "/login.html",
            templateUrl: "www/views/login.html",
            data: {pageTitle: '登陆'},
            params: {'toState': null},
            controller: "LoginController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/css/login/login.css'
                        ]
                    });
                }]
            }
        })
        .state('regedit', {
            url: "/regedit.html",
            templateUrl: "www/views/regedit.html",
            data: {pageTitle: '注册'},
            controller: "regeditController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/regeditController.js'
                        ]
                    });
                }]
            }
        })
        .state('index', {
            url: "/index.html",
            templateUrl: "www/views/dashboard.html",
            data: {pageTitle: '首页'},
            controller: "dashboardController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/dashboardController.js'
                        ]
                    });
                }]
            }
        })
        .state('medias', {
            url: "/medias.html",
            templateUrl: "www/views/medias.html",
            data: {pageTitle: '媒体查询'},
            controller: "mediasController",
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/mediasController.js'
                        ]
                    });
                }]
            }
        })
        .state('bid', {
            url: "/bid/{bidId}.html",
            templateUrl: "www/views/bid.html",
            params: {'bidId': null},
            data: {pageTitle: '竞价明细'},
            controller: "bidController",
            auth:false,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/bidController.js'
                        ]
                    });
                }]
            }
        })
        .state('addPublish', {
            url: "/publish/addPublish.html",
            templateUrl: "www/views/addPublish.html",
            params: {'model': null},
            data: {pageTitle: '新增上刊单'},
            controller: "addPublishController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load([{
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', 
                        files: [
                            'www/js/controllers/addPublishController.js'
                        ]
                    }]);
                }]
            }
        })
        
        .state('viewPublish', {
            url: "/publish/viewPublish.html",
            templateUrl: "www/views/viewPublish.html",
            params: {'model': null},
            data: {pageTitle: '查看上刊单'},
            controller: "addPublishController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before',
                        files: [
                            'www/js/controllers/addPublishController.js'
                        ]
                    });
                }]
            }
        })
        
        .state('resource', {
            url: "/resource.html",
            templateUrl: "www/views/resource.html",
            controller: "resourceController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/resourceController.js'
                        ]
                    });
                }]
            }
        })
        .state('publish', {
            url: "/publish.html",
            templateUrl: "www/views/publish.html",
            controller: "publishController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/publishController.js'
                        ]
                    });
                }],
                showImgFiles: ['$ocLazyLoad', function($ocLazyLoad) {// 动态加载图片显示需要的文件
                    return $ocLazyLoad.load([{
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', 
                        files: [
                            /* 用于图片效果css 开始*/
                            'assets/global/plugins/fancybox/source/jquery.fancybox.css',
                            'assets/global/css/portfolio.css',
                            /*用于图片效果css  结束*/
                        	/*用于图片显示js 开始*/
                            'assets/global/scripts/portfolio.js',
                            'assets/global/plugins/fancybox/source/jquery.fancybox.pack.js',
                            'assets/global/plugins/jquery-mixitup/jquery.mixitup.min.js'
                            /* 图片显示js 结束*/
                        ]
                    }]).then(function(){
                    	Portfolio.init();// 图片效果初始化
                    });
                }]
            }
        })
        .state('userBasic', {
            url: "/userBasic.html",
            templateUrl: "www/views/userBasic.html",
            controller: "userBasicController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/userBasicController.js'
                        ]
                    });
                }]
            }
        })
        
        .state('myBids', {
            url: "/myBids.html",
            templateUrl: "www/views/myBids.html",
            controller: "myBidsController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/myBidsController.js'
                        ]
                    });
                }]
            }
        })
        
        .state('publishMakeBill', {
            url: "/publishMakeBill.html",
            templateUrl: "www/views/publishMakeBill.html",
            controller: "publishMakeBillController",
            params: {'model': null},
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
                        files: [
                            'www/js/controllers/publishMakeBillController.js'
                        ]
                    });
                }]
            }
        })

        .state('publishMakeBillDetail', {
            url: "/publishMakeBillDetail/publishMakeBillDetail.html",
            templateUrl: "www/views/publishMakeBillDetail.html",
            params: {'model': null},
            data: {pageTitle: '制作单明细'},
            controller: "publishMakeBillDetailController",
            auth:true,
            resolve: {
                deps: ['$ocLazyLoad', function($ocLazyLoad) {
                    return $ocLazyLoad.load({
                        name: 'App',
                        insertBefore: '#ng_load_plugins_before', 
                        files: [
                            'www/js/controllers/publishMakeBillDetailController.js'
                        ]
                    });
                }]
            }
        })
        
}]);
